Have you ever attended a wedding ceremony? When the bridegroom and bride smilingly go through the carpet with beautiful flowers holding in the bride’s hands, we also cannot ignore the best men and bridesmaids. Even all the people put their eyes on the bridegroom and bride, and it seems no one gives his /her attention to the best men and bridesmaids. The best men and bridesmaids are indispensable for a wedding ceremony in that they play important roles in assisting the ceremony. The chief assistant holds the wedding ring until it is exchanged. While header and footer are like a silent assistant that adds some additional but relatively necessary information in the word document. They are in the top and bottom margins of each page.Header and footer can contain much information such as the file name and the path of your document, text or graphics, page numbers, chapter titles, dates, company logo, author’s name and so on. In Spire.Doc, using Visual Studio to insert header and footer with C#, VB.NET is simple if you follow the steps below. Let us look at picture 1.1:
Before you start, make sure that Spire.Doc and Visual Studio are installed in your computer.
Spire.Doc, is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight.
You can Freely Download Spire.Doc.
Spire.Doc, is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight.
You can Freely Download Spire.Doc.
Please folow the procedure below:
First, create the Header and Footer project.
Open Visual Studio (2008 or later)—File—New Project—Visual C#—Windows Forms Application. Name Windows Forms Application Header and Footer(if you need to create a Visual Basic project, Windows Forms Application, you can name the project Header and Footer)—OK.
Second, add a button.
After building project Header and Footer in Form 1, you need to add a button in Form1.
Third, add references.
Open Solution Explorer--—Click project —Add Reference—Find "C:\Program Files\e-iceblue\Spire.Doc"(the default folder which you installed the Spire Doc in )— Double-click the folder Bin.
If the target framework of the project
Net 2.0, double-click folder NET2.0
or Net 3.5, double-click folder NET3.5
or Net 4.0, double-click folder NET4.0
Select assembly Spire Doc dll —Click OK
First, create the Header and Footer project.
Open Visual Studio (2008 or later)—File—New Project—Visual C#—Windows Forms Application. Name Windows Forms Application Header and Footer(if you need to create a Visual Basic project, Windows Forms Application, you can name the project Header and Footer)—OK.
Second, add a button.
After building project Header and Footer in Form 1, you need to add a button in Form1.
Third, add references.
Open Solution Explorer--—Click project —Add Reference—Find "C:\Program Files\e-iceblue\Spire.Doc"(the default folder which you installed the Spire Doc in )— Double-click the folder Bin.
If the target framework of the project
Net 2.0, double-click folder NET2.0
or Net 3.5, double-click folder NET3.5
or Net 4.0, double-click folder NET4.0
Select assembly Spire Doc dll —Click OK
Fourth, view the method.
After you create the project Header and Footer, double-click “button1”, you can see the code view and the following method has been added automatically:
C#
private void button1_Click(object sender, EventArgs e)
VB.NET
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
After you create the project Header and Footer, double-click “button1”, you can see the code view and the following method has been added automatically:
C#
private void button1_Click(object sender, EventArgs e)
VB.NET
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Fifth, add the following codes to the top of the file:
C#
using Spire.Doc;
using Spire.Doc.Documents;
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
C#
using Spire.Doc;
using Spire.Doc.Documents;
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Sixth, add the following codes to the method.
C#
private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
InsertHeaderFooter(document);
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void InsertHeaderFooter(Document document)
{
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a header and footer into a document.")
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
section.PageSetup.DifferentFirstPageHeaderFooter = true;
paragraph = new Paragraph(document);
paragraph.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph);
paragraph = new Paragraph(document);
paragraph.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserverd").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph);
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
C#
private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
InsertHeaderFooter(document);
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void InsertHeaderFooter(Document document)
{
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a header and footer into a document.")
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
section.PageSetup.DifferentFirstPageHeaderFooter = true;
paragraph = new Paragraph(document);
paragraph.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph);
paragraph = new Paragraph(document);
paragraph.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserverd").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph);
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
VB.NET
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
'Create word document
Dim document_Renamed As New Document()
InsertHeaderFooter(document_Renamed)
'Save doc file.
document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End Sub
Private Sub InsertHeaderFooter(ByVal document_Renamed As Document)
Dim section_Renamed As Section = document_Renamed.AddSection()
Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
paragraph_Renamed.AppendText("The sample demonstrates how to insert a header and footer into a document.")
paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)
section_Renamed.PageSetup.DifferentFirstPageHeaderFooter = True
paragraph_Renamed = New Paragraph(document_Renamed)
paragraph_Renamed.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15
paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
section_Renamed.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph_Renamed)
paragraph_Renamed = New Paragraph(document_Renamed)
paragraph_Renamed.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserverd").CharacterFormat.FontSize = 15
paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
section_Renamed.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph_Renamed)
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
'Create word document
Dim document_Renamed As New Document()
InsertHeaderFooter(document_Renamed)
'Save doc file.
document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End Sub
Private Sub InsertHeaderFooter(ByVal document_Renamed As Document)
Dim section_Renamed As Section = document_Renamed.AddSection()
Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
paragraph_Renamed.AppendText("The sample demonstrates how to insert a header and footer into a document.")
paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)
section_Renamed.PageSetup.DifferentFirstPageHeaderFooter = True
paragraph_Renamed = New Paragraph(document_Renamed)
paragraph_Renamed.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15
paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
section_Renamed.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph_Renamed)
paragraph_Renamed = New Paragraph(document_Renamed)
paragraph_Renamed.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserverd").CharacterFormat.FontSize = 15
paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
section_Renamed.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph_Renamed)
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub
Finally, operate the project Header and Footer.
In Solution Explorer, right-click the project Header and Footer —Click Debug—Start new instance, you can see the opened window Form1—Click button 1, you can see picture 7.1 shows itself:
In Solution Explorer, right-click the project Header and Footer —Click Debug—Start new instance, you can see the opened window Form1—Click button 1, you can see picture 7.1 shows itself:
Word header and footer provide people areas to insert special information needed by both authors and readers in the document. They enjoy great popularity among book editors. When people read the foreign original classics, many slang words have to be explained in the footer. For some professional books, many new specialized vocabularies should be interpreted in the footer. For many companies, they offer exclusive books only allowed to be read by their own staff. This kind of books is commonly printed the company logo in the header or footer of each page. Inserting page number either at the left or right or center of the header or footer can be surveyed everywhere. However, not all documents need to insert header and footer. Header and footer are often used in the long document or in special cases.
More Word Functions
More Word Functions